package com.example.map;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

public class MainActivity extends Activity{
    private MapView mapView;
    private RadioButton rd1,rd2;
    private CheckBox cb1,cb2;
    private BaiduMap bdmap=null;
    private LocationClient mLocationClient;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initEvent();
        bdmap=mapView.getMap();
        bdmap.setMyLocationEnabled(true);
        LocationClientOption option=new LocationClientOption();
        option.setOpenGps(true);
        option.setCoorType("bd0911");
        option.setScanSpan(1000);
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
        mLocationClient.start();
    }


    private void initEvent() {
		// TODO Auto-generated method stub
		rd1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
		        bdmap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
		        rd2.setChecked(false);
			}
		});
        rd2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				bdmap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
				rd1.setChecked(false);
			}
		});
        cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(cb1.isChecked()){
					bdmap.setTrafficEnabled(true);
					bdmap.setCustomTrafficColor("#ffba0101", "#fff33131", "#ffff9e19", "#00000000");
					MapStatusUpdate u=MapStatusUpdateFactory.zoomTo(13);
					bdmap.animateMapStatus(u);
				}
				else{bdmap.setTrafficEnabled(false);}
			}
		});
        cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(cb2.isChecked()){
					bdmap.setBaiduHeatMapEnabled(true);
				}
				else{bdmap.setBaiduHeatMapEnabled(false);}
			}
		});
    }


	private void init() {
		// TODO Auto-generated method stub
    	 mapView = (MapView) findViewById(R.id.bmapView);
    	 rd1=(RadioButton) findViewById(R.id.radioNormal);
    	 rd2=(RadioButton) findViewById(R.id.radioWeixing);
    	 cb1=(CheckBox) findViewById(R.id.radioJiaotong);
    	 cb2=(CheckBox) findViewById(R.id.radioReli);
    	 mLocationClient=new LocationClient(this);
	}


	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	mapView.onResume();
    	super.onResume();
    }
    @Override
    protected void onPause() {
    	// TODO Auto-generated method stub\
    	mapView.onPause();
    	super.onPause();
    }
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	mLocationClient.stop();
    	bdmap.setMyLocationEnabled(false);
    	mapView=null;
    	mapView.onDestroy();
    	super.onDestroy();
    }
    public class MyLocationListener extends BDAbstractLocationListener{

		@Override
		public void onReceiveLocation(BDLocation arg0) {
			// TODO Auto-generated method stub
			if(arg0==null||mapView==null){
				return;
			}
			MyLocationData locData=new MyLocationData.Builder()
			                           .accuracy(arg0.getRadius())
			                           .direction(arg0.getDirection())
			                           .latitude(arg0.getLatitude())
			                           .longitude(arg0.getLongitude())
			                           .build();
			bdmap.setMyLocationData(locData);
		}
    	
    }
}
